package com.sky.demo.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.realogic.btp.dhtmlx.DhtmlxGridXml;
import org.realogic.cfm.KAppContext;
import org.realogic.cfm.domain.KEnumItem;
import org.realogic.cfm.domain.KPageResult;
import org.realogic.cfm.persistence.query.QueryCondition;
import org.realogic.cfm.persistence.query.QueryConditionFactory;
import org.realogic.cfm.spring.SpringWebHolder;
import org.realogic.cfm.util.LongUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sky.demo.entity.EntityOne;
import com.sky.demo.service.EntityOneService;

@Controller
@RequestMapping("/entityOne")
public class EntityOneController {

	private EntityOneService entityOneService;

	public EntityOneService getEntityOneService() {
		return entityOneService;
	}

	@Autowired
	public void setEntityOneService(EntityOneService entityOneService) {
		this.entityOneService = entityOneService;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute(new EntityOne());
		List<KEnumItem> tmpList = new ArrayList<KEnumItem>(2);
		tmpList.add(new KEnumItem("0", "否"));
		tmpList.add(new KEnumItem("1", "是"));
		model.addAttribute("yesNoEnums", tmpList);
		return "plugin/demo/EntityOneEdit";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Validated EntityOne aValue) {
//		aValue.setId(LongUUID.uuidOfAbsLongs());
//		KAppContext.attachCreatedInfo(aValue);
//		// 补充新增信息
//		entityOneService.create(aValue);
		return "redirect:../main.do#page/entityOne/list.do";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(@RequestParam(value = "key", required = true) Long id, Model model) {
		model.addAttribute("entityOne", entityOneService.read(id));
		List<KEnumItem> tmpList = new ArrayList<KEnumItem>(2);
		tmpList.add(new KEnumItem("0", "否"));
		tmpList.add(new KEnumItem("1", "是"));
		model.addAttribute("yesNoEnums", tmpList);
		return "plugin/demo/EntityOneEdit";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(EntityOne aValue, BindingResult result) {
		// 补充修改信息
		KAppContext.attachUpdatedInfo(aValue);
		entityOneService.update(aValue);
		return "redirect:/entityOne/list.do";
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public String remove(@RequestParam(value = "key", required = true) Long id) {
		entityOneService.remove(id);
		return "success";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "plugin/demo/EntityOneList";
	}
	
	

	@RequestMapping(value = "/datagrid")
	@ResponseBody
	public DhtmlxGridXml datagrid() {
		KPageResult result = null;
		DhtmlxGridXml xmler = null;
		HttpServletRequest request = SpringWebHolder.getRequest();
		QueryCondition condition = QueryConditionFactory.createQueryCondition(request);
		String grid = request.getParameter("grid");
		try {
			result = entityOneService.query(condition);
			if (grid == null || "".equals(grid)) {
				xmler = new DhtmlxGridXml("EntityOneGrid", result,KAppContext.currentUser());
			} else {
				xmler = new DhtmlxGridXml(grid, result,KAppContext.currentUser());
			}
			return xmler;
			
		} catch (Exception e) {
			return null;
		}
	}

}